#include "precompiled.h"
#include "utility.h"
using namespace weo::engine;
using namespace weo::engine::item;
using namespace weo::engine::utility;
using namespace std;

std::string roundprec(float value,int digits)
{
    float val = floor(value*pow(10.0,digits)+0.5f)/pow(10.0,digits);
    std::string str;
    std::ostringstream osr;
    osr << val;
    return osr.str();
}
template<typename T>
std::string doRichText(const T& value)
{
    std::string sToolTip;
    sToolTip  = "<table cellspacing=2 cellpadding=0>";
    sToolTip += "<tr><td align=center colspan=2><nobr><b>";
    sToolTip += value.Name();
    sToolTip += "</b></nobr></td></tr>";
    sToolTip += "<tr><td><nobr>";
    sToolTip += "Id: "+boost::lexical_cast<std::string>(value.Id());
    sToolTip += "</nobr></td></tr>";
    sToolTip += "<tr><td><nobr>";
    sToolTip += "Level: "+boost::lexical_cast<std::string>(value.Level());
    sToolTip += "</nobr></td></tr>";
    vector<weo::engine::item::Attribute>::const_iterator it;
    for(it = value.Attributes()->begin();it!= value.Attributes()->end();it++)
    {
        std::string name = weo::engine::item::KeyValues[it->Type()];
        sToolTip += "<tr><td><nobr>";
        sToolTip += Utility::toString(name,it->Value());
        sToolTip += "</nobr></td></tr>";
    }
    sToolTip += "<tr><td><nobr>";

    sToolTip += "</table>";
    return sToolTip;
}
template<>
std::string Utility::toRichText<Item>(const Item& value)
{
    return doRichText(value);
}
template<>
std::string Utility::toRichText<Enchant>(const Enchant& value)
{
    return doRichText(value);
}
std::string Utility::toString(const std::string key, const float value)
{
    std::string retval;
    if(key == "ExpertiseRating")
    {
        retval = "Expertise: "+roundprec(value/ExpertiseRating,2);
    }
    else if(key == "HitRating")
    {
        retval = "Hit: "+roundprec(value/HitRating,2)+"%";
    }
    else if(key == "HitSpellRating")
    {
        retval = "HitSpell: "+roundprec(value/HitSpellRating,2)+"%";
    }
    else if(key == "CritRating")
    {
        retval = "Crit: "+roundprec(value/CritRating,2)+"%";
    }
    else if(key == "HasteRating")
    {
        retval = "Haste: "+roundprec(value/HasteRating,2)+"%";
    }
    else if(key == "HasteSpellRating")
    {
        retval = "HasteSpell: "+roundprec(value/HasteSpellRating,2)+"%";
    }
    else if(key == "CritSpellRating")
    {
        retval = "CritSpell: "+roundprec(value/CritSpellRating,2)+"%";
    }
    else if(key == "DefenseRating")
    {
        retval = "Defense: "+roundprec(value/DefenseRating,2);
    }
    else if(key == "DodgeRating")
    {
        retval = "Dodge: "+roundprec(value/DodgeRating,2)+"%";
    }
    else if(key == "ParryRating")
    {
        retval = "Parry: "+roundprec(value/ParryRating,2)+"%";
    }
    else if(key == "BlockRating")
    {
        retval = "Block: "+roundprec(value/BlockRating,2)+"%";
    }
    else if(key == "Special")
    {
        retval = "Special: "+boost::lexical_cast<std::string,unsigned int>((unsigned int) value);
    }
    else
    {
        retval = key +": "+roundprec(value,2);
    }
    return retval;
}